<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>String Template Resources</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="resources.html" title="Chapter 16. Resources">
<link rel="prev" href="resources.html" title="Chapter 16. Resources">
<link rel="next" href="resources.streams.html" title="Stream Template Resources">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">String Template Resources</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="resources.html">Prev</a> </td>
<th width="60%" align="center">Chapter 16. Resources</th>
<td width="20%" align="right"> <a accesskey="n" href="resources.streams.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="String Template Resources">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="resources.string"></a>String Template Resources</h2></div></div></div>
<p>
  Smarty can render templates from a string by using the <code class="literal">string:</code> or <code class="literal">eval:</code> resource. 
 </p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>
    The <code class="literal">string:</code> resource behaves much the same as a template file. 
    The template source is compiled from a string and stores the compiled template code for later reuse. 
    Each unique template string will create a new compiled template file. 
    If your template strings are accessed frequently, this is a good choice. 
    If you have frequently changing template strings (or strings with low reuse value), 
    the <code class="literal">eval:</code> resource may be a better choice, as it doesn't save compiled templates to disk.
   </p></li>
<li class="listitem"><p>
    The <code class="literal">eval:</code> resource evaluates the template source every time a page is rendered. This is a good choice
    for strings with low reuse value. If the same string is accessed frequently, the <code class="literal">string:</code> resource may
    be a better choice.
   </p></li>
</ul></div>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
   With a <code class="literal">string:</code> resource type, each unique string generates a compiled file. Smarty cannot detect a string that has changed,
   and therefore will generate a new compiled file for each unique string. It is important to choose the correct resource so that you do not
   fill your disk space with wasted compiled strings.
  </p>
</div>
<div class="example">
<a name="id455869"></a><p class="title"><b>Example 16.5. Using templates from strings</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php
$smarty-&gt;assign('foo','value');
$template_string = 'display {$foo} here';
$smarty-&gt;display('string:'.$template_string); // compiles for later reuse
$smarty-&gt;display('eval:'.$template_string); // compiles every time
?&gt;

  </pre>
<p>From within a Smarty template</p>
<pre class="programlisting">

{include file="string:$template_string"} {* compiles for later reuse *}
{include file="eval:$template_string"} {* compiles every time *}


  </pre>
</div>
</div>
<br class="example-break"><p>
     Both <code class="literal">string:</code> and <code class="literal">eval:</code> resources may be encoded with
     <a class="ulink" href="http://php.net/urlencode" target="_top"><code class="varname">urlencode()</code></a> or
     <a class="ulink" href="http://php.net/urlencode" target="_top"><code class="varname">base64_encode()</code></a>. This is
     not necessary for the usual use of <code class="literal">string:</code> and <code class="literal">eval:</code>,
     but is required when using either of them in conjunction with 
     <a class="link" href="resources.extends.html" title="Extends Template Resources"><code class="varname">Extends Template Resource</code></a>
 </p>
<div class="example">
<a name="id455951"></a><p class="title"><b>Example 16.6. Using templates from encoded strings</b></p>
<div class="example-contents">
<pre class="programlisting">
 
 &lt;?php
 $smarty-&gt;assign('foo','value');
 $template_string_urlencode = urlencode('display {$foo} here');
 $template_string_base64 = base64_encode('display {$foo} here');
 $smarty-&gt;display('eval:urlencode:'.$template_string_urlencode); // will decode string using urldecode()
 $smarty-&gt;display('eval:base64:'.$template_string_base64); // will decode string using base64_decode()
 ?&gt;
 
   </pre>
<p>From within a Smarty template</p>
<pre class="programlisting">
 
 {include file="string:urlencode:$template_string_urlencode"} {* will decode string using urldecode() *}
 {include file="eval:base64:$template_string_base64"} {* will decode string using base64_decode() *}

 
   </pre>
</div>
</div>
<br class="example-break">
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="resources.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="resources.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="resources.streams.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 16. Resources </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Stream Template Resources</td>
</tr>
</table>
</div>
</body>
</html>
